/*
 * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
 * http://www.gnu.org/licenses/lgpl-3.0.html
 */

#ifndef CBEXCEPTION_H
#define CBEXCEPTION_H

#include <wx/string.h>
//引用wxSafeShowMessage()函数
#include <wx/log.h>
// 引用cbC2U()函数
#include "globals.h"

/**
* 实现功能: Code::Blocks错误处理单元.
* 定义了两个宏以帮助在Code::Blocks中处理错误:
* cbThrow() 和 cbAssert().
* 注释:
* cbAsser()在发布模式下不执行任何操作.对于调试模式,使用-DcbDEBUG编译
*/

/** 实现功能: 基Code::Blocks异常对象 */
class DLLIMPORT cbException
{
    public:
        cbException(const wxString& msg, const wxString& file, int line);
        virtual ~cbException();

        /**
         * 实现功能: 显示异常错误消息.
         * 参数说明: 参数safe如果为true,将使用wxSafeShowMessage显示错误,否则将使用普通消息框.
         */
        void ShowErrorMessage(bool safe = true);

        // 公共成员
        wxString Message; ///< 异常的错误消息.
        wxString File; ///< 引发异常的文件.
        int Line; ///< 文件中引发异常的行.
};

#if wxUSE_UNICODE
    #define cbThrow(message) throw cbException(message, cbC2U(__FILE__), __LINE__)
#else
    #define cbThrow(message) throw cbException(message, __FILE__, __LINE__)
#endif

#ifndef cbDEBUG
    #define cbAssert(expr)
#else
    // In unix we use kill to terminate the application, that makes gdb
    // keep it alive which makes debugging easier.
    // (thanks go to an unknown author)
    #ifdef __WXMSW__
        #define DIE() exit(1)
    #else
        #include <csignal>
        #define DIE() kill(getpid(), SIGTERM)
    #endif

    #if wxUSE_UNICODE
        #define cbAssertMessage(expr) \
            wxString err; \
            err.Printf(_T("Assertion failed in %s at %s:%d.\n\n%s"), cbC2U(__PRETTY_FUNCTION__).wx_str(), cbC2U(__FILE__).c_str(), __LINE__, cbC2U(#expr).c_str());
    #else
        #define cbAssertMessage(expr) \
            wxString err; \
            err.Printf(_T("Assertion failed in %s at %s:%d.\n\n%s"), __PRETTY_FUNCTION__, __FILE__, __LINE__, #expr);
    #endif

    // non-fatal assertion
    #define cbAssertNonFatal(expr) \
        if (!(expr)) \
        { \
            cbAssertMessage(expr); \
            wxSafeShowMessage(_T("Assertion error"), err); \
        }

    // fatal assertion
    #define cbAssert(expr) \
        if (!(expr)) \
        { \
            cbAssertMessage(expr); \
            wxSafeShowMessage(_T("Fatal assertion error"), err); \
            DIE(); \
        }
#endif

#endif // CBEXCEPTION_H
